package com.lebang.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.lebang.common.Login;
import com.lebang.entity.member.LbbMember;
import com.lebang.example.MemberNewTradeExample;
import com.lebang.model.enums.buss.TaskCategory;
import com.lebang.service.buss.MemberService;
import com.lebang.service.sys.MemberNewTradeService;
import com.lebangweb.common.TextUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


/**
 * 用户签到数据更改
 *
 * @author hwq
 * @date 2020-03-27
 */
@Controller
@RequestMapping("/sys/membernewtrade")
public class MemberNewTradeController {
    @Autowired
    private MemberNewTradeService memberNewTradeService;
    @Autowired
    private MemberService memberService;

    @GetMapping("/list")
    public String list(String memberId, Model model) {
        model.addAttribute("memberId", memberId);
        return "/membernewtrade/list";
    }
    @GetMapping("/datalist")
    public String data(String memberId, Model model) {
        model.addAttribute("list", TaskCategory.values());
        return "/membernewtrade/data";
    }
    @ApiOperation(value = "分页")
    @PostMapping("/list")
    @ResponseBody
    @Login
    public JSONObject list(HttpServletRequest request, @RequestParam Integer page, @RequestParam Integer limit
            , String memberId) {
        JSONObject jsonObject = new JSONObject();
        MemberNewTradeExample example = new MemberNewTradeExample();
        page = (page - 1) * limit;
        example.setOffset(page.longValue());
        example.setLimit(limit);
        example.createCriteria().andMemberIdEqualTo(memberId);
        example.setOrderByClause("create_time desc");
        jsonObject.put("code", 0);
        jsonObject.put("data", memberNewTradeService.selectByExample(example));
        jsonObject.put("count", memberNewTradeService.countByExample(example));
        return jsonObject;
    }

    @ApiOperation(value = "分页")
    @PostMapping("/datalist")
    @ResponseBody
    public JSONObject list(HttpServletRequest request,
                           @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime star,
                           @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end,
                           String category,
                           String phone, String imei, String masterPhone, String appStore) {
        JSONObject jsonObject = new JSONObject();
        MemberNewTradeExample example = new MemberNewTradeExample();
        MemberNewTradeExample.Criteria criteria = example.createCriteria();
        if (TextUtil.isEmpty(star, end)) {
            LocalDate now = LocalDate.now();
            star = LocalDateTime.of(now, LocalTime.MIN);
            end = LocalDateTime.of(now, LocalTime.MAX);
        }
        criteria.andCreateTimeBetween(star, end);
        if(!TextUtil.isEmpty(category)){
            criteria.andCategoryEqualTo(category);
        }
        if(!TextUtil.isEmpty(imei)){
            criteria.andImeiEqualTo(imei);
        }
        if(!TextUtil.isEmpty(masterPhone)){
            List<String> list=memberService.selectMasterMemberIdList(masterPhone);
            if(list!=null&&list.size()!=0){
                criteria.andMemberIdIn(list);
            }else {
                list=new ArrayList<>();
                list.add("123");
                criteria.andMemberIdIn(list);
            }
        }
        if(!TextUtil.isEmpty(phone)){
            Optional<LbbMember> member=Optional.ofNullable(memberService.selectByPhone(phone));

            criteria.andMemberIdEqualTo(member.map(LbbMember::getMemberId).orElse("123"));
        }
        if(!TextUtil.isEmpty(appStore)){
            criteria.andAppStoreEqualTo(appStore);
        }
        jsonObject.put("code", 0);
        jsonObject.put("data", memberNewTradeService.groupByExample(example));
        jsonObject.put("count", 10);
        return jsonObject;
    }
}
